﻿using System;
using System.Data;
using System.Data.Common;

namespace wojilu.Data
{
    /// <summary>
    /// Sqlite 数据工厂，获取 Connection, Command, DataAdapter
    /// </summary>
    public class SqliteFactory : DbFactoryBase
    {
        private static ISqliteHelper GetSqliteHelper()
        {
            return SqliteHelperLoader.GetHelper();
        }

        public static Type sqLiteCommandType
        {
            get { return GetSqliteHelper().GetSqliteCommandType(); }
        }

        public static Type sqLiteConnectionType
        {
            get { return GetSqliteHelper().GetSqliteConnectionType(); }
        }

        public override IDbConnection GetConnection(String connectionString)
        {
            return GetSqliteHelper().GetSqliteConnection(connectionString);
        }

        public override IDbCommand GetCommand(String CommandText)
        {
            base.checkOpen();
            IDbCommand cmd = GetSqliteHelper().GetSqliteCommand();
            cmd.Connection = cn;
            cmd.CommandText = CommandText;
            base.setTransaction(cmd);
            return cmd;
        }

        internal override IDatabaseChecker GetDatabaseChecker()
        {
            return new SqliteDatabaseChecker();
        }

        public override IDatabaseDialect GetDialect()
        {
            return new SqliteDialect();
        }

        public override Object SetParameter(IDbCommand cmd, String parameterName, Object parameterValue)
        {

            if (parameterValue == null) return parameterValue;

            parameterValue = base.processValue(parameterValue);
            parameterName = new SqliteDialect().GetParameterAdder(parameterName);

            IDbDataParameter parameter = GetSqliteHelper().GetSqliteParameter(parameterName, parameterValue);
            cmd.Parameters.Add(parameter);

            return parameterValue;

        }

        public override DbDataAdapter GetAdapter()
        {
            return GetSqliteHelper().GetSqliteDataAdapter(cmd);
        }

        public override DbDataAdapter GetAdapter(String CommandText)
        {
            return GetSqliteHelper().GetSqliteDataAdapter(GetCommand(CommandText));
        }

    }
}
